package com.avic.rong.activity.product;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.text.TextUtils;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.avic.rong.BaseActivity;
import com.avic.rong.R;
import com.avic.rong.utils.SharedPreferencesUtil;
import com.avic.rong.utils.StatusBarUtil;
import com.orhanobut.logger.Logger;

import java.net.URLEncoder;

import butterknife.BindView;
import butterknife.OnClick;
import wendu.dsbridge.DWebView;

public class H5Activity extends BaseActivity {


    public static String ARG_URL = "arg_url";

    @BindView(R.id.webView)
    DWebView webView;


    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_back)
    ImageView iv_back;
    @BindView(R.id.progress_bar)
    ProgressBar progress_bar;
    @BindView(R.id.ll_root)
    LinearLayout ll_root;

    private String articleUrl;
    private String mTitleStr = "详情页";


    @Override
    protected int getContentViewId() {
        return R.layout.activity_h5;
    }


    @Override
    protected void initView() {


        articleUrl = getIntent().getStringExtra(ARG_URL);
        tvTitle.setText(mTitleStr);

        webView.addJavascriptObject(this, null);
        if (Build.VERSION.SDK_INT >= 21) {
            webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        webView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                return true;
            }
        });
        webView.setWebChromeClient(new MyWebChromeClient());
        webView.loadUrl(articleUrl);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });


    }

    private class MyWebChromeClient extends WebChromeClient {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            progress_bar.setProgress(newProgress);
            if (newProgress == 100) {
                progress_bar.setVisibility(View.GONE);
            }
            super.onProgressChanged(view, newProgress);
        }

        @SuppressLint("ResourceAsColor")
        @Override
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);
            if (!TextUtils.isEmpty(title)) {
                mTitleStr = title;
                tvTitle.setText(title);
                if ("排行榜".equals(mTitleStr)) {
                    tvTitle.setTextColor(Color.BLACK);
                    ll_root.setBackgroundResource(R.color.white);
                    iv_back.setColorFilter(Color.BLACK);
                    StatusBarUtil.setStatusBarColor(H5Activity.this, Color.WHITE);
                }else{
                    tvTitle.setTextColor(Color.WHITE);
                    ll_root.setBackgroundResource(R.mipmap.top_bar_bg);
                    iv_back.setColorFilter(Color.WHITE);
                }
            } else {
                tvTitle.setText(mTitleStr);
            }
        }
    }

    //同步获取编码token
    @JavascriptInterface
    public String getLoginUserEncode(Object msg) {
        String login_token = "";
        if (SharedPreferencesUtil.isLogin()) {
            try {
                login_token = URLEncoder.encode(SharedPreferencesUtil.getLoginToken(), "UTF-8");
            } catch (Exception e) {
                Logger.i(e.getMessage());
            }
        }
        return login_token;
    }


    @OnClick({R.id.iv_back})
    public void clickDirect(View view) {
        int viewId = view.getId();
        switch (viewId) {
            case R.id.return_back:
                if (webView.canGoBack()) {
                    webView.goBack();
                    return;
                }
                onBackPressed();
                break;

        }
    }


    @Override
    protected void addListener() {

    }


    public static Intent queryIntent(Context context, String url) {
        Intent intent = new Intent(context, H5Activity.class);
        intent.putExtra(ARG_URL, url);
        return intent;
    }
}
